package com.qiaolang.dao;

import com.qiaolang.db.DB;
import com.qiaolang.pojo.Sign;
import com.qiaolang.pojo.Users;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class UserDAO {
    public Connection conn = null;
    public UserDAO() {
        conn = DB.getConnection();
    }
    public Users login(String StuNo) {//不需要的到权限之后再登陆
        PreparedStatement pstmt;
        ResultSet rs;
        Users users=new Users();
        try {
            pstmt = conn.prepareStatement("select * from users where userno=?");
            pstmt.setString(1, StuNo);
            rs = pstmt.executeQuery();
            while (rs.next()) {
               users.setUserno(rs.getString(1));
               users.setUsername(rs.getString(2));
               users.setRole(rs.getString(3));
               users.setDatatime(rs.getString(4));
            }
            rs.close();
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }
    public List queryAll() {
        PreparedStatement pstmt;
        ResultSet rs;
        List list=new ArrayList<>();
        try {
            pstmt = conn.prepareStatement("select * from users");
            rs = pstmt.executeQuery();
            Users users=null;
            while (rs.next()) {
                users=new Users();
                users.setUserno(rs.getString(1));
                users.setUsername(rs.getString(2));
                users.setRole(rs.getString(3));
                users.setDatatime(rs.getString(4));
                list.add(users);
            }
            rs.close();
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public boolean qianDao(String userno) {
        PreparedStatement pstmt;
        String sql="insert into sign values(?,?)";
        boolean flag=false;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,userno);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String datetime=df.format(new java.util.Date()).toString();
            pstmt.setString(2,datetime);

            flag=pstmt.executeUpdate()>0?true:false;
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public String chaxun(String userno) {
        PreparedStatement pstmt;
        ResultSet rs;
        String sql="select count(*) from sign where userno=?";
        String count="";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,userno);
            rs = pstmt.executeQuery();
            while (rs.next()) {
             count=rs.getString(1);
            }
            rs.close();
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}

